package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.service.IBookService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.example.springboot.common.Result;
import com.example.springboot.entity.Book;

import org.springframework.web.bind.annotation.RestController;

/**
* BookAPI接口
*/
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private IBookService bookService;

    /**
     * 新增
     */
    @PostMapping
    public Result save(@RequestBody Book book) {
        bookService.save(book);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping
    public Result update(@RequestBody Book book) {
        bookService.updateById(book);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        bookService.removeById(id);
        return Result.success();
    }

    /**
     * 查询全部数据
     */
    @GetMapping
    public Result findAll() {
        return Result.success(bookService.list());
    }

    /**
     * 查询详情
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(bookService.getById(id));
    }

    /**
     * 分页查询数据
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<Book>().orderByDesc(Book::getId);
        queryWrapper.like(StrUtil.isNotBlank(name), Book::getName, name);
        Page<Book> page = bookService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    /**
     * 分页查询数据 - 前端
     */
    @GetMapping("/page/front")
    public Result findPageFront(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer categoryId,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<Book>().orderByDesc(Book::getId);
        queryWrapper.eq(Book::getIsShelf,"0");
        if(StrUtil.isNotBlank(name)){
            queryWrapper.and(wrapper -> wrapper
                    .like(Book::getName, name)
                    .or()
                    .like(Book::getAuthor, name)
                    .or()
                    .like(Book::getIsbn, name)
            );
        }
        queryWrapper.eq(Objects.nonNull(categoryId) && categoryId != 0, Book::getCategoryId, categoryId);
        Page<Book> page = bookService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    /**
     * 最新图书
     */
    @GetMapping("/newBook")
    public Result newBook() {
        return Result.success(bookService.newBook());
    }

    /**
     * 最热图书
     */
    @GetMapping("/hotBook")
    public Result hotBook() {
        return Result.success(bookService.hotBook());
    }

    @GetMapping("/bookCount")
    public Result bookCount() {
        return Result.success(bookService.bookCount());
    }

    @GetMapping("/borrowCount")
    public Result borrowCount() {
        return Result.success(bookService.borrowCount());
    }
}
